import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/homeworks.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/bookGridWithCheckBox.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonAddBooksToFolder.dart';
import 'package:intl/intl.dart';
import 'package:universal_html/html.dart' as uHtml;

class HomeworkDetailedBooks extends StatefulWidget {
  HomeworkDetailedBooks(this.assignmentId, this.lang, this.language);

  final String assignmentId;
  final String lang;
  final String language;

  @override
  State<HomeworkDetailedBooks> createState() => _HomeworkDetailedBooksState();
}

class _HomeworkDetailedBooksState extends State<HomeworkDetailedBooks> {
  final box = Hive.box('appconfig');
  final BooksController _booksController = Get.find();
  final HomeworksController _assignmentsController = Get.find();
  late String classId;
  final PageStyles _pageStyles = PageStyles();

  @override
  void initState() {
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    if (isStudent) {
      classId = box.get(CacheManagerKey['currentclass'].toString());
      if (GetPlatform.isWeb) {
        uHtml.window.localStorage['lang'] = this.widget.lang;
        uHtml.window.localStorage['language'] = this.widget.language;
        uHtml.window.sessionStorage['assignmentId'] = widget.assignmentId;
      } else {
        box.put('assignmentId', widget.assignmentId);
      }
      _booksController.setCurrentClassId(classId);
      _booksController.setCurrentAssignmentId(widget.assignmentId);
      Future.delayed(Duration(milliseconds: 100), (() {
        _booksController.fetchAssignmentBookItemsById(
            classId, widget.assignmentId);
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build student AssignmentDetailedBooks');

    return Container(
      decoration: _pageStyles.pageBoxDecoration(),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          leading: BackButton(
            onPressed: () {
              _booksController.assignedBookItems.clear();
              _assignmentsController.fetchStudentLiveHomeworks(classId);
              Navigator.of(context).pop();
            },
          ),
          title: Text('Assigned Books'.tr),
        ),
        body: Container(
          margin: EdgeInsets.all(12.0),
          child: Center(
            child: _booksController.obx(
              (data) => Column(
                children: [
                  Row(
                    children: [
                      Text('Assigment Name'.tr),
                      Text(_booksController.assignmentDetail.name ?? 'None'.tr),
                      SizedBox(width: 25),
                      Text('Due by'.tr),
                      Text(
                        DateFormat('MM/dd/yyyy').format(
                            DateTime.fromMillisecondsSinceEpoch(
                                _booksController.assignmentDetail.endDate!)),
                      ),
                      SizedBox(width: 25),
                      Text('Required to Completed'.tr),
                      Text(
                          '${_booksController.assignmentDetail.noOfBookToBeRead ?? 'None'.tr}'),
                      SizedBox(width: 25),
                      Text('Instruction'.tr),
                      Text(_booksController.assignmentDetail.instruction ??
                          'None'.tr),
                    ],
                  ),
                  SizedBox(height: 15),
                  Expanded(
                    child: Row(
                      children: [
                        Expanded(
                          child: BookGridWithCheckBox(
                            books: _booksController.assignedBookItems,
                            assignmentId: widget.assignmentId,
                            readingType: NanhaiBookReadingTypes.assignment,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              onEmpty: Text('Data not available ...'.tr),
              onLoading: Container(),
            ),
          ),
        ),
        floatingActionButton: FloatingActionButtonAddBookToFolder(
            readingType: NanhaiBookReadingTypes.assignment),
      ),
    );
  }
}
